package com.tanhua.app.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.AnnouncementApi;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.FriendApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private AnnouncementApi announcementApi;

    public ResponseEntity addContacts(Long friendId) {
        // 1、向mongo中保存好友
        friendApi.save(UserHolder.getUserId(), friendId);
        // 2、向环信中保存好友
        huanXinTemplate.addContacts(UserHolder.getUserId(), friendId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        // 1、调用api查询好友分页
        PageBeanVo pageBeanVo = friendApi.findByPage(pageNum, pageSize, UserHolder.getUserId());

        // 2、封装userInfo = vo
        List<ContactVo> voList = new ArrayList<>();
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                ContactVo vo = new ContactVo();
                UserInfo friendInfo = userInfoApi.findByUserId(friend.getFriendId());
                BeanUtil.copyProperties(friendInfo, vo); // 设置好友基本信息
                vo.setUserId(friend.getFriendId().toString()); // 设置好友id
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);

    }

    public ResponseEntity findAnnouncementsByPage(Integer pageNum, Integer pageSize) {
        // 1、调用api实现分页查询
        PageBeanVo pageBeanVo = announcementApi.findByPage(pageNum, pageSize);

        // 2、封装vo对象
        List<AnnouncementVo> voList = new ArrayList<>();
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(announcementList)) {
            for (Announcement announcement : announcementList) {
                AnnouncementVo vo = new AnnouncementVo();
                BeanUtil.copyProperties(announcement, vo);
                vo.setCreateDate(DateUtil.format(announcement.getCreated(), "yyyy-MM-dd HH:mm"));
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
